import 'package:cloud_medicine/components/medicine_edit&add.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/model/shop_list.dart';
import 'package:cloud_medicine/pages/shopmanage_upimage.dart';
import 'package:flutter/material.dart';

class MedicineAdd extends StatefulWidget {
  MedicineAdd({Key key}) : super(key: key);

  @override
  _MedicineAddState createState() => _MedicineAddState();
}

class _MedicineAddState extends State<MedicineAdd> {
  DateTime lastPopTime;

  TextEditingController priceController = new TextEditingController();
  TextEditingController nameController = new TextEditingController();
  TextEditingController descriController = new TextEditingController();
  TextEditingController numberController = new TextEditingController();

  bool _loading = false;
  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }

    return;
  }

  @override
  void dispose() {
    super.dispose();
    priceController.dispose();
    nameController.dispose();
    descriController.dispose();
    numberController.dispose();
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  _childLayout() {
    if (_loading) {
      return Center(
        child: Container(
          child: CircularProgressIndicator(),
        ),
      );
    } else {
      return ListView(
        children: [
          _imgWidget(),
          MedicineEditAndAdd(
            nameController: nameController,
            priceController: priceController,
            describeController: descriController,
            saveInfoAndCommit: () {
              Navigator.pop(context);
            },
            oldname: '',
            oldprice: '',
            olddescribe: '',
            numberController: numberController,
            oldnumber: 0,
            id: 2,
          )
        ],
      );
    }
  }

  _entryImgPick() {
    Navigator.of(context).push(MaterialPageRoute(
        builder: (BuildContext context) => ShopManageImgUp(
              pagekind: 1,
            )));
  }

  _imgWidget() {
    List<Widget> _myWidget = [
      GestureDetector(
        child: Container(
          width: 100,
          height: 100,
          decoration: BoxDecoration(
              border: Border.all(width: 1, color: Colors.lightBlueAccent)),
          child: Icon(Icons.add, color: Colors.lightBlueAccent),
        ),
        onTap: () => _entryImgPick(),
      )
    ];
    if (medicineImgUp.isNotEmpty) {
      setState(() {
        medicineImgUp.map((e) {
          _myWidget
              .add(Container(width: 100, height: 100, child: Image.network(e)));
        }).toList();
      });
    }
    return Wrap(
      children: _myWidget,
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          if (lastPopTime == null ||
              DateTime.now().difference(lastPopTime) > Duration(seconds: 1)) {
            lastPopTime = DateTime.now();
            openAlertDialog(context, () {
              Navigator.pop(context);
            }, '确认退出编辑，一旦退出，一切将不予保存');
            return new Future.value(false);
          }
          return new Future.value(true);
        },
        child: Scaffold(
          appBar: AppBar(
            title: Text('添加药品'),
          ),
          body: _childLayout(),
        ));
  }
}
